﻿Imports System.IO
Imports System.Net

Public Class gaysComm

#Region "Declarations"

    Private pUsername As String
    Private pPassword As String

    Public Sub New(ByVal Username As String, ByVal Password As String)
        pUsername = Username
        pPassword = Password
    End Sub

#End Region

#Region "Properties"

    Public Property Username() As String
        Get
            Return pUsername
        End Get
        Set(ByVal value As String)
            pUsername = value
        End Set
    End Property



    Public Property Password() As String
        Get
            Return pPassword
        End Get
        Set(ByVal value As String)
            pPassword = value
        End Set
    End Property

#End Region

    ''' <summary>
    ''' Führt simples GET-Request gegen eine URL aus und gibt Response direkt zurück
    ''' </summary>
    ''' <param name="Url">URL die aufgerufen werden soll</param>
    ''' <returns>HTTP Response</returns>
    ''' <remarks></remarks>
    Private Function simpleHttpGetRequest(ByVal Url As String) As String
        Debug.Print("simpleHttpGetRequest()" + vbCrLf + "Request: " + Url)

        Try

            Dim request As WebRequest = WebRequest.Create(Url)
            request.Method = "GET"
            Dim dataStream As Stream
            Dim response As WebResponse = request.GetResponse()
            dataStream = response.GetResponseStream()
            Dim reader As New StreamReader(dataStream)
            Dim responseFromServer As String = reader.ReadToEnd()
            reader.Close()
            dataStream.Close()
            response.Close()

            Debug.Print("Response: " + responseFromServer)

            Return responseFromServer
        Catch ex As Exception
            Return Nothing
        End Try
    End Function

    Private Function genQueryString(ByVal functionName As String, Optional ByVal additionalParms As String = "") As String
        Return baseurl + "apiv2/" + functionName + "/apiNickname/" + Username + "/apiPassword/" + Password + "/" + additionalParms
    End Function

    Private Sub jsonDecode(ByVal jsonString As String, ByRef targetObject As Object)
        Debug.Print("jsonDecode()" + jsonString)
        Try
            ' deserialisieren
            Json.JsonConvert.PopulateObject(jsonString, targetObject)
            Debug.Print("Deserialisierung abgeschlossen")
        Catch ex As Exception
            ' fehler aufgetreten, möglicherweise ein serverfehler?
            Try
                ' dann wird immer ein SimpleResult zurückgegeben
                Dim pSimpleResult As New objSimpleResult
                Json.JsonConvert.PopulateObject(jsonString, pSimpleResult)

                ' nachricht vorhanden?
                If pSimpleResult.message <> "" Then
                    Throw New Exception("GAYS.DE meldet: " + pSimpleResult.message)
                Else
                    Throw New Exception("GAYS.DE konnte die Anforderung nicht verarbetien")
                End If
            Catch ex2 As Exception
                ' jetzt ist was komplett schiefgelaufen: exception durchreichen
                Throw ex2
            End Try
        End Try
    End Sub

    Public Function getGlobalStatus() As objGlobalStatus
        Dim pGlobalStatus As New objGlobalStatus

        jsonDecode(simpleHttpGetRequest(genQueryString("globalStatus")), CType(pGlobalStatus, Object))

        Return pGlobalStatus
    End Function

    ''' <summary>
    ''' Prüft, ob Zugangsdaten korrekt sind
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function checkCredentials() As Boolean
        Dim pRes As New objSimpleResult

        jsonDecode(simpleHttpGetRequest(genQueryString("checkCredentials")), CType(pRes, Object))

        Return pRes.result
    End Function

    Public Sub setOnlineStatus(ByVal Status As Integer)
        simpleHttpGetRequest(genQueryString("setOnlineStatus", "onlineStatus/" + Status.ToString))
    End Sub

End Class
